package com.servlet;

import com.beans.Cateinfo;
import com.beans.Goodsinfo;
import com.beans.PageInfo;
import com.dao.CateDao;
import com.dao.GoodsDao;
import com.jdbc.PageUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/GoodsServlet") @MultipartConfig
public class GoodsServlet extends HttpServlet {
    private GoodsDao goodsDao = new GoodsDao();
    private CateDao cateDao = new CateDao();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String flag = request.getParameter("flag");
        if("manage".equals(flag)){
            manage(request,response);
        }else if ("duoshan".equals(flag)){
            duoshan(request,response);
        }else if ("delete".equals(flag)){
            delete(request,response);
        }else if ("add".equals(flag)){
            add(request,response);
        }else if("checkadd".equals(flag)){
            checkadd(request,response);
        }else if("show".equals(flag)){
            show(request,response);
        }else if("srea".equals(flag)){
            srea(request,response);
        }else if("update".equals(flag)){
            update(request,response);
        }else if("updateinfo".equals(flag)){
            updateinfo(request,response);
        }else if ("addn".equals(flag)) {
            addn(request, response);
        }else if("smallc".equals(flag)){
            smallc(request,response);
        }
    }

    private void smallc(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int parentId=Integer.parseInt(request.getParameter("parentId"));
        List<Cateinfo> slist= cateDao.getslist(parentId);
        JSONArray s= JSONArray .fromObject(slist);
        response.getWriter().print(s);
    }

    private void addn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Cateinfo> blist= cateDao.getslist();
        request.setAttribute("blist",blist);
        request.getRequestDispatcher("/goods/goods-add.jsp").forward(request, response);
    }

    private void updateinfo(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String goodsName=request.getParameter("goodsName");
        int bigCateId=Integer.parseInt(request.getParameter("bigCateId"));
        int smallCateId=Integer.parseInt(request.getParameter("smallCateId"));
        String unit=request.getParameter("unit");
        Double price=Double.parseDouble(request.getParameter("price"));
        String producter=request.getParameter("producter");
        String des=request.getParameter("des");
        Goodsinfo goods=new Goodsinfo();
        goods.setGoodsName(goodsName);
        goods.setBigCateId(bigCateId);
        goods.setSmallCateId(smallCateId);
        goods.setUnit(unit);
        goods.setPrice(price);
        goods.setProducter(producter);
        goods.setDes(des);
        Part part=request.getPart("pic");
        InputStream in= part.getInputStream();
        byte pictureData[]=new byte[in.available()];
        in.read(pictureData);
        goods.setPictureData(pictureData);
        int i= goodsDao.update(goods);
        manage(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        Goodsinfo goodsinfo= goodsDao.getgoodsbyid(id);
        request.setAttribute("goodsinfo",goodsinfo);
        request.getRequestDispatcher("/goods/goods-update.jsp").forward(request, response);
    }

    private void srea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        Goodsinfo goodsinfo= goodsDao.getgoodsbyid(id);
        request.setAttribute("goodsinfo",goodsinfo);
        request.getRequestDispatcher("/goods/goods-sreach.jsp").forward(request, response);
    }

    private void show(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        Goodsinfo goodsinfo= goodsDao.getgoodsbyid(id);
        byte pic[]=goodsinfo.getPictureData();
        response.setContentType("Image/jpg");
        ServletOutputStream out = response.getOutputStream();
        out.write(pic);
        out.flush();
    }

    private void checkadd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name= (String)request.getParameter("goodsName");
        Goodsinfo goodsinfo= goodsDao.getAdminNamebyid(name);
        if(goodsinfo==null)
            response.getWriter().print(0);
        else
            response.getWriter().print(1);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String goodsName=request.getParameter("goodsName");
        int bigCateId=Integer.parseInt(request.getParameter("bigCateId"));
        int smallCateId=Integer.parseInt(request.getParameter("smallCateId"));
        String unit=request.getParameter("unit");
        Double price=Double.parseDouble(request.getParameter("price"));
        String producter=request.getParameter("producter");
        String des=request.getParameter("des");

        Goodsinfo goods=new Goodsinfo();
        goods.setGoodsName(goodsName);
        goods.setBigCateId(bigCateId);
        goods.setSmallCateId(smallCateId);
        goods.setUnit(unit);
        goods.setPrice(price);
        goods.setProducter(producter);
        goods.setDes(des);
        Part part=request.getPart("pic");
        InputStream in= part.getInputStream();
        byte pictureData[]=new byte[in.available()];
        in.read(pictureData);
        goods.setPictureData(pictureData);
        int i=goodsDao.addGoods(goods);
        if(i>0) {
            request.setAttribute("goodsId",i);
            request.setAttribute("msg", "添加成功");
            request.getRequestDispatcher("/goods/goods-add.jsp").forward(request, response);
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        goodsDao.del(id);
        manage(request,response);
    }

    private void duoshan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idlist=request.getParameter("id");
        String sid[]= idlist.split(" ");
        for(int i=0;i<sid.length;i++){
            goodsDao.del(Integer.parseInt(sid[i]));
            System.out.println(sid[i]);
        }
        manage(request,response);
    }

    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bigC=request.getParameter("bigC");
        String smallC=request.getParameter("smallC");
        String goods=request.getParameter("goods");
        int pageIndex=1;
        int pageSize=6;
        int rowCount= GoodsDao.getGoodsCount(bigC,smallC,goods);

        String pageIndexStr=request.getParameter("pageIndex");
        if(pageIndexStr!=null) {
            pageIndex=Integer.parseInt(pageIndexStr);
        }
        PageInfo page= PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<Goodsinfo> goodslist= goodsDao.getGooodsList(page,bigC,smallC,goods);
        List<Cateinfo> blist= cateDao.getslist();
        request.setAttribute("blist",blist);
        request.setAttribute("goodslist", goodslist);
        request.setAttribute("page", page);
        request.setAttribute("bigCate",bigC);
        request.setAttribute("smallCate",smallC);
        request.setAttribute("goods",goods);
        request.getRequestDispatcher("goods/goods-manage.jsp").forward(request, response);
    }
}
